package user;

import utils.JsonOut;
import utils.MysqlUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.HashMap;

@WebServlet(name = "ListFansServlet")
public class ListFansServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        HttpSession session = request.getSession();

        String email = UserSession.getUserSessionEmail(session);

        response.setCharacterEncoding("UTF-8");
        response.setHeader("Content-type", "text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
//        if (email == null || email.isEmpty()){
//            out.println(JsonOut.error(220, "not_login"));
//            return;
//        }
//
//        ResultSet userSet = (new MysqlUtils()).getUserInfo(email);
//        if (userSet == null){
//            out.println(JsonOut.error(101, "not_exist"));
//            return;
//        }

        String userId = request.getParameter("user_id");

        String fromNum = request.getParameter("m");
        String toNum = request.getParameter("n");

        if (fromNum == null || fromNum.isEmpty()) {
            fromNum = "0";
        }

        if (toNum == null || toNum.isEmpty()){
            toNum = "10";
        }

        int fromInt = 0;
        int toInt = 10;

        try{
            fromInt = Integer.parseInt(fromNum);
            toInt = Integer.parseInt(toNum);
            if (toInt < fromInt) toInt = fromInt;
        }catch (Exception e){
            e.printStackTrace();
        }

        ResultSet userSet; // = (new MysqlUtils()).getFollowList(email, fromInt, toInt);

        boolean isUserIdEmpty = false;
        boolean getMyInfo = false;
        if (userId == null || userId.isEmpty()){
            isUserIdEmpty = true;
        }

        if (email == null || email.isEmpty()){
            if (isUserIdEmpty){
                out.println(JsonOut.error(220, "not_login"));
                return;
            }

            // 查询userId的资料
            userSet = (new MysqlUtils()).getUserIdInfo(userId);
        }else {
            if (isUserIdEmpty){
                getMyInfo = true;
                userSet = (new MysqlUtils()).getUserInfo(email);
            }else {
                String myId = (new MysqlUtils()).getUserId(email);

                if (myId != null && !myId.isEmpty()){
                    if (myId.equals(userId)) {
                        getMyInfo = true;
                    }
                }
                if (getMyInfo){
                    userSet = (new MysqlUtils()).getUserInfo(email);
                }else {
                    userSet = (new MysqlUtils()).getUserIdInfo(userId);
                }
            }
        }


        if (userSet == null){
            out.println(JsonOut.error(101, "not_exist"));
            return;
        }

        ResultSet ret;
        if (getMyInfo) ret = (new MysqlUtils()).getFansList(email, fromInt, toInt);
        else ret = (new MysqlUtils()).getIdFansList(userId, fromInt, toInt);

        //ResultSet ret = (new MysqlUtils()).getFansList(email, fromInt, toInt);

        try {
            ArrayList<HashMap<String, String>> array = new ArrayList<>();
            while (ret.next()){
                HashMap<String, String> item = new HashMap<>();

                // from_id,nickname,area,sex,head_image,time,birthday,signature
                String thisUserId = ret.getString(1);
                String nickname = ret.getString(2);
                String area = ret.getString(3);
                String sex = ret.getString(4);
                String headImage = ret.getString(5);
                String followTime = ret.getString(6);
                String birthday = ret.getString(7);
                String signature = ret.getString(8);
                item.put("user_id", thisUserId != null ? thisUserId : "");
                item.put("nickname", nickname != null ? nickname : "");
                item.put("area", area != null ? area : "");
                item.put("sex", sex != null ? sex : "");
                item.put("head_image", headImage != null ? headImage : "");
                item.put("follow_time", followTime != null ? followTime : "");
                item.put("birthday", birthday != null ? birthday : "");
                item.put("signature", signature != null ? signature : "");

                if (getMyInfo){
//                    item.put("follow_state", "fans");
                    String followState = (new MysqlUtils()).getFollowStatus(email, thisUserId);
                    item.put("follow_state", followState != null ? followState : "");
                }else {
                    if (email != null && !email.isEmpty()) {
//                        System.out.println("logined: "+email);
                        String followState = (new MysqlUtils()).getFollowStatus(email, thisUserId);
                        item.put("follow_state", followState != null ? followState : "");
                    }
                }

                array.add(item);
            }

            out.println(JsonOut.common(0, "fans_list", array));
        } catch (Exception e) {
            e.printStackTrace();
            out.println(JsonOut.error(500, "sql_sth_wrong"));
        }
    }
}
